<template>
    <div>
        <Form ref="form" :model="form" :label-width="80" :rules="ruleValidate">
            <FormItem label="标题" prop="title.zh">
                <Input v-model="form.title.zh" placeholder="请填写标题"></Input>
            </FormItem>
            <FormItem label="选择教师" prop="examine_user">
                <Input v-model="exname" @on-focus="modal=true" placeholder="请选择教师" style="width: 200px"></Input>
            </FormItem>
            <FormItem>
                <Button type="primary" @click="submit('form')">保存</Button>
            </FormItem>
        </Form>
        <Modal v-model="modal" title="选择教师" @on-cancel="form.examine_user=''" width="700" class-name="nimei">
            <!-- <auditor @on-select="(id)=> {this.form.examine_user=id}"></auditor> -->
            <select-personal @user-list="userlist"></select-personal>
        </Modal>
    </div>
</template>

<script>
    export default {
        components: {
            SelectPersonal: resolve => {
                require([
                    "./SelectPersonal.vue"
                ], resolve);
            },
        },
        data() {
            return {
                years: [], //学年学期
                time_an: null, //开始时间
                time_en: null, //结束时间
                yearlist: [],
                addUrl: 'api/teacher/v3/attendance_management/add',
                form: {
                    title: {
                        zh: null,
                        en: null,
                        mn: null,
                    },
                    examine_user: '',
                },
                modal: false,
                ruleValidate: {
                    'title.zh': [{
                        required: true,
                        message: '标题不能为空',
                        trigger: 'blur'
                    }],
                    //   'examine_user': [
                    // 		{ required: true, message: '教师不能为空', trigger: 'blur'}
                    // 	]
                },
                examiners: [],
                examiners_copy: [],
                dutie_id: '',
                examinersList: [],
                teacherList: '',
                usersList: [],
                exname: ""
    
    
            }
        },
    
        created() {
    
            this.getExamine()
    
        },
        methods: {
            getExamine() {
                this.$http.get('api/user/v1/get/teacher/user/list').then(val => {
                    this.examinersList = val.data
                })
            },
            userlist(data) {
                let add = []
                this.examinersList.forEach(el => {
                    if (data.includes(el.user_id)) {
                        add.push(el.teacher_name.zh)
                    }
                })
                this.teacherList = JSON.stringify(add)
                this.usersList = data
                this.exname = add.join(",")
                // console.log(this.exname,this.usersList,add)
            },
            //提交时
            submit(name) {
                this.$refs[name].validate((valid) => {
    
                    if (valid) {
                        console.log(this.usersList)
                        if (this.usersList.length < 1) {
                            this.$Message.error('教师不能为空');
                            return
                        }
                        let formData = {
                            title: {
                                zh: this.form.title.zh
                            },
                            teacher: this.usersList
                        }
                        console.log(formData)
                        this.$http.post(this.addUrl, formData).then(val => {
                            if (val.code == '200') {
                                this.$emit('showParent', false)
                                this.$emit('backList', true)
                            }
                        }, err => console.log(err))
                    } else {
                        this.$Message.error('验证失败');
                    }
                })
    
    
            },
            time_anfun(obj) {
                this.form.time_an = obj
    
            },
            time_enfun(obj) {
                this.form.time_en = obj
    
            },
    
    
        },
        mounted() {
    
    
        },
    
    }
</script>

<style>
    .nimei .ivu-modal-content {
        height: 650px
    }
    
    .nimei .ivu-modal-content .ivu-modal-body {
        height: 500px
    }
    
    .ivu-modal-wrap .ivu-modal {
        height: 650px
    }
    
    .ivu-modal-body {
        height: 650px;
    }
</style>
